﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace MII_FIF.Layouts.MII_FIF.DataKunjungan
{
    public partial class ViewDataKunjungan : LayoutsPageBase
    {
        SqlConnection conn;
        DataTable dt;
        SqlDataAdapter adapt;
        //string connStr = "Data Source=sp2013-server;Initial Catalog=InventoryManagement;User ID=sa;Password=P@ssw0rd";
        string queryStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.DataKunjunganGroupName))
            {
                btnAdd.Click += btnAdd_Click;
                //btnDelete.Click += btnDelete_Click;
                GridKunjungan.RowCommand += GridKunjungan_RowCommand;

                if (!IsPostBack)
                {
                    initialize();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        protected DataTable ExecuteDataTable(string queryStr)
        {
            //conn = new SqlConnection(connStr);
            conn = new SqlConnection(Configuration.DBConnection());
            conn.Open();
            dt = new DataTable();
            adapt = new SqlDataAdapter(queryStr, conn);
            adapt.Fill(dt);
            conn.Dispose();
            return dt;
        }

        void initialize()
        {
            divstatus.Visible = false;

            if (Request.QueryString["Status"] == "Submitted")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Data has been Submitted & Waiting for approval";
                divstatus.Visible = true;
            }
            if (Request.QueryString["Status"] == "Removed")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Data has been Removed";
                divstatus.Visible = true;
            }
            if (Request.QueryString["Status"] == "Approved")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Task has been Approved";
                divstatus.Visible = true;
            }
            if (Request.QueryString["Status"] == "Rejected")
            {
                ViewState["Status"] = Request.QueryString["Status"].ToString();
                lblStatus.Text = "Task has been Rejected";
                divstatus.Visible = true;
            }
            LoadPS();
        }

        void LoadPS()
        {
            try
            {

                queryStr = @"EXEC SP_SelectDataKunjungan";

                dt = ExecuteDataTable(queryStr);
                GridKunjungan.DataSource = dt;
                GridKunjungan.DataBind();

                ViewState["DTKunjungan"] = dt;
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/DataKunjungan/FormDataKunjungan.aspx"));
        }

        //void btnDelete_Click(object sender, EventArgs e)
        //{
        //    bool delete = false;
        //    DataTable DTKunjungan = ((DataTable)ViewState["DTKunjungan"]);
        //    foreach (GridViewRow row in GridKunjungan.Rows)
        //    {
        //        CheckBox chk = (CheckBox)row.FindControl("chk");
        //        if (chk != null)
        //            if (chk.Checked)
        //            {
        //                delete = true;
        //                break;
        //            }

        //    }
        //    if (delete)
        //    {
        //        for (int i = GridKunjungan.Rows.Count - 1; i > -1; i--)
        //        {
        //            CheckBox chk = (CheckBox)GridKunjungan.Rows[i].FindControl("chk");
        //            if (chk != null)
        //                if (chk.Checked)
        //                {
        //                    queryStr = @"SP_DeleteDataKunjungan '" + DTKunjungan.Rows[i]["ID"].ToString() + "'";
        //                    ExecuteDataTable(queryStr);
        //                    DTKunjungan.Rows.RemoveAt(i);
        //                }
        //        }
        //        GridKunjungan.DataSource = DTKunjungan;
        //        GridKunjungan.DataBind();
        //        ViewState["DTKunjungan"] = DTKunjungan;
        //    }
        //    else
        //    {
        //        lblError.Text += "Please check at least 1 item to delete <br />";
        //    }
        //}

        void GridKunjungan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Approval_DataKunjungan.aspx?ID=" + e.CommandArgument));
            }
        }

        protected void GridKunjungan_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            GridKunjungan.PageIndex = e.NewPageIndex;
            LoadPS();
        }


    }
}
